<?php
/**
 * User: Allen
 * Date: 15-10-15
 * 报价控制器
 */

class Quote extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('quote_model');
		$this->load->model('insurance_model');
		$this->load->model('company_model');
	}
	
	/**
	 * @brief报价信息页面
	 * @param Null
	 * @par 2015/10/15 Ver 1.00 Created by Allen
	 */	
	public function quote_list() {
		$this->data['vehicle_owner'] =  empty($_POST) ? '' : $_POST['vehicle_owner'];
		$this->data['car_num'] =  empty($_POST) ? '' : $_POST['car_num'];
		$this->load->view('/www/quote_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取报价信息
	 * @param Null
	 * @par 2015/10/15 Ver 1.00 Created by Allen
	 */
	public function ajax_quote_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$data['vehicle_owner'] ? $where['vehicle.vehicle_owner like'] = '%'.trim($data['vehicle_owner']).'%' :'';
		$data['car_num'] ? $where['vehicle.car_num like'] = '%'.trim($data['car_num']).'%' :'';
		$where['concat(quote.id) like'] = '%'.trim($search).'%';
		$this->data['count'] = $this->quote_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$quote_list = $this->quote_model->getQuoteList($where,$length,$start,$order_by);

			foreach($quote_list as $key => $value) {
				if($value['quote_time']) {
					$quote_list[$key]['quote_time'] = date('Y-m-d H:i',$quote_list[$key]['quote_time']);
					$add_url = '<a href="/www/quote/view_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">查看报价</span></a>';
				}else {
					$quote_list[$key]['quote_time'] = '';
					$add_url = '<a href="/www/quote/add_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">添加报价</span></a>';
				}
				if($value['status'] == 1) {
					$del_oper = "onclick=\"if(confirm('确实要冻结此条记录吗？')) return true;else return false; \"";
					$status = 0;
					$del_text = '冻结';
				}else {
					$del_oper = "onclick=\"if(confirm('确实要解除冻结此条记录吗？')) return true;else return false; \"";
					$status = 1;
					$del_text = '解冻';
					
				}			
				$edit_url = '<a href="/www/quote/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-success">详情</span></a>';
				$del_url = '<a href="/www/quote/del?id='.$value['id'].'&status='.$status.'"  '.$del_oper.'><span class="btn btn-xs btn-danger cancel">'.$del_text.'</span></a>';
				$quote_list[$key]['operate'] = $edit_url." ".$add_url." ".$del_url;
			}
			$aaData = $quote_list;	
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/15 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['quote.id'] = $id;
		$quote_info = $this->quote_model->checkQuote($where);
		$offer_info = unserialize($quote_info['offer']);
		$id_list = $offer_info['id'];
		$sum_list = $offer_info['sum'];
		$version_where['status'] = 1;
		$insurance_list = $this->insurance_model->getInsuranceAll($version_where);
		$this->data['data'] = $quote_info;
		$this->data['insurance_list'] = $insurance_list;
		$this->data['id_list'] = $id_list;
		$this->data['sum_list'] = $sum_list;
		$this->load->view('/www/quote_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/16 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$sum_list = $data['sum'];
		$id_list = $data['id_list'];
		$array = array();	
		foreach($sum_list as $key => $value) {
			$info = explode(',', $value);
			$id = $info[0];
			$array['id'][] = $info[0];
			if(in_array($id, $id_list)) {
				$array['sum'][] = $info[1];	
			}else {
				$array['sum'][] = 0;
			}
		}
		$data['offer'] = serialize($array);
		unset($data['id_list']);unset($data['sum']);
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->quote_model->editQuote($where,$data);
		$go_url = '/www/quote/quote_list';
		if($edit_result) {
			$this->location_href($this->lang->line('edit_success'),$go_url);	
		}else {
			$this->location_href($this->lang->line('edit_false'),$go_url);	
		}
	}
	
	/**
	 * @brief 删除报价信息
	 * @param Null
	 * @par 2015/10/16 Ver 1.00 Created by Allen
	 */
	public function del() {
		$data = $_GET;
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$del_result = $this->quote_model->editQuote($where,$data);
		$go_url = '/www/quote/quote_list';
		if($del_result) {
			$this->location_href($this->lang->line('operate_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('operate_false'),$go_url);
		}
	}

	/**
	 * @brief 进入报价页面
	 * @param Null
	 * @par 2015/10/16 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$id = $this->uri->segment(4);



		$where['quote.id'] = $id;
		$quote_info = $this->quote_model->checkQuote($where);	
		$offer_info = unserialize($quote_info['offer']);
		$id_list = $offer_info['id'];
		$sum_list = $offer_info['sum'];
		$insurance_list = array();
		foreach($id_list as $key => $value) {
			if($sum_list[$key] != 0) {
				$insurance_where['id'] = $value;
				$insurance_info = $this->insurance_model->checkInsurance($insurance_where);
				$insurance_info['user_sum'] = $sum_list[$key];
				$insurance_list[] = $insurance_info;
			}
		}
		$version_where['status'] = 1;
		$company_list = $this->company_model->getInsureAll($version_where);
		$this->data['data'] = $quote_info;
		$this->data['insurance_list'] = $insurance_list;
		$this->data['company_list'] = $company_list;
		$this->load->view('/www/quote_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;
		$data['quote_data']['insurance_id'] = $data['quote_data']['insurance_id'][0];
		$data['quote_data'] = serialize($data['quote_data']);
		$data['quote_time'] = time();
		$where['id'] = $data['id'];	

		//确定报价并报价下一个
		unset($data['save']);
		$go_url = '/www/quote/quote_list';
		if(isset($data['next'])){
			$next = $this->quote_model->getQuoteNext(array("quote.id >"=>$data['id']));
			if($next){
				$go_url = '/www/quote/add_page/'.$next['id'];
			}
			unset($data['next']);
		}


		$edit_result = $this->quote_model->editQuote($where,$data);
		
		if($edit_result) {
			$this->location_href($this->lang->line('add_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('add_false'),$go_url);
		}
	}
	
	/**
	 * @brief 获取会员报价方案
	 * @param Null
	 * @par 2015/10/15 Ver 1.00 Created by Allen
	 */
	public function ajax_user_quote() {
		$id = $this->uri->segment(4);
		$where['quote.id'] = $id;
		$quote_info = $this->quote_model->checkQuote($where);
		$offer_info = unserialize($quote_info['offer']);
		$id_list = $offer_info['id'];
		$array = array();
		foreach($id_list as $key => $value) {
			$insurance_where['id'] = $value;
			$insurance_info = $this->insurance_model->checkInsurance($insurance_where);
			$array[$key]['name'] = $insurance_info['name'];
			$array[$key]['sum'] = $offer_info['sum'][$key];
		}
		$str = '';
		foreach($array as $k => $v) {
			$quote_choose = '已选择';
			if($v['sum'] == 0) {
				$quote_choose = '未选择';
			}
			$str .='<tr><td style="border:1px solid #ddd;">险种:'.$v['name'].'</td><td style="border:1px solid #ddd;">状态:'.$quote_choose.'</td><td style="border:1px solid #ddd;">保额:'.$v['sum'].'</td></tr>';
		}
		$str = "<table style='border:1px solid #ddd;' width='30%'>".$str."</table>";
		$json_data['str'] = $str;
		$json_data['data'] = $array;
		echo json_encode($json_data);
	}
	
	/**
	 * @brief 进入查看报价页面
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function view_page() {
		$id = $this->uri->segment(4);
		$where['quote.id'] = $id;
		$quote_info = $this->quote_model->checkQuote($where);
		$offer_info = unserialize($quote_info['offer']);
		$quote_info['quote_data'] = unserialize($quote_info['quote_data']);
		$id_list = $offer_info['id'];
		$sum_list = $offer_info['sum'];
		$insurance_list = array();
		foreach($id_list as $key => $value) {
			if($sum_list[$key] != 0) {
				$insurance_where['id'] = $value;
				$insurance_info = $this->insurance_model->checkInsurance($insurance_where);
				$insurance_info['user_sum'] = $sum_list[$key];
				$insurance_list[] = $insurance_info;
			}
		}
		$company_array = array();
		foreach($quote_info['quote_data']['company_id'] as $key => $value) {
			$version_where['id'] = $value;
			$company_info = $this->company_model->checkInsure($version_where);
			$company_array[$key] = $company_info;
			$company_array[$key]['insurance_list'] = $insurance_list;
			foreach($company_array[$key]['insurance_list'] as $k => $v) {
				if(in_array($v['id'],$quote_info['quote_data']['insurance_id'])) {
					$offset = array_search($v['id'],$quote_info['quote_data']['insurance_id']);
					$company_array[$key]['insurance_list'][$k]['quote_num'] = $quote_info['quote_data']['sum_id'][$key][$offset];
				}else {
					$company_array[$key]['insurance_list'][$k]['quote_num'] = 0;
				}
			}			
			$company_array[$key]['remark'] = $quote_info['quote_data']['remark'][$key];
		}
		$this->data['data'] = $quote_info;
		$this->data['company_array'] = $company_array;
		$this->load->view('/www/quote_view',$this->data);
	}	
	
	/**
	 * @brief 修改报价
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function view() {
		$data = $_POST;
		$data['quote_data']['insurance_id'] = $data['quote_data']['insurance_id'][0];
		$data['quote_data'] = serialize($data['quote_data']);
		$data['quote_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->quote_model->editQuote($where,$data);
		$go_url = '/www/quote/quote_list';
		if($edit_result) {
			$this->location_href($this->lang->line('add_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('add_false'),$go_url);
		}
	}
}
?>